package pCloud.service;

import java.util.ArrayList;

import pCloud.crypto.kpabe.CipherMessage;
import pCloud.crypto.kpabe.KPAbe;
import pCloud.crypto.kpabe.TransformedCipherMessage;

/**
 * @author ttadinh
 * Handling sliding window stuff. One object for every sliding-window 
 * policy
 */
public class CloudSlidingWindowHandle {
	KPAbe crypto;
	int beta;
	int temp;
	byte[][] tmpCiphers;
	byte[][] tmpEps;
	int streamId;
	int userId;
	int windowIndex;
	public CloudSlidingWindowHandle(KPAbe c, int b, int id, int uid){
		this.crypto = c;
		this.beta = b;		
		temp=0;		
		this.streamId = id;
		this.userId = uid;
		this.windowIndex = 1;
	}
	
	//null means no transmission to the user
	public TransformedCipherMessage compute(byte[] cpht, byte[] ept, int idx){
		if (temp==0){
			tmpCiphers = new byte[beta][cpht.length];
			tmpEps = new byte[beta][ept.length];
		}
		tmpCiphers[temp] = cpht;
		tmpEps[temp] = ept;
		temp++;		
		if (temp<beta)
			return null;
		else{
								
			byte[] cph = this.crypto.multiplyCiphers(tmpCiphers, streamId);							
						
			byte[] ep = this.crypto.multiplyCiphers(tmpEps, streamId);			
			TransformedCipherMessage chm = new TransformedCipherMessage(
					streamId, cph, ep, true, windowIndex, idx);
			windowIndex++;
			temp = 0;
			return chm;
		}
	}
}
